package com.bp.hearthealth.pulse.lnapps.bp_db.bp_user

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface BpUserDao {
    @Query("SELECT * FROM user")
    suspend fun getUserList(): List<BpUser>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertUser(user: BpUser)

    @Query("SELECT * FROM user WHERE id = :id")
    suspend fun getUser(id: String): BpUser?

    @Query("UPDATE user SET heartRateLevel = :heartRateLevel WHERE id = :id")
    suspend fun updateHeartRateLevel(id: String, heartRateLevel: String)

    @Query("UPDATE user SET isFirstOpen = :isFirstOpen, birthday = :birthday, gender = :gender WHERE id = :id")
    suspend fun updateBirthdayAndGenderInfo(id: String, isFirstOpen: Boolean, birthday: String, gender: Int): Int

    @Query("UPDATE user SET birthday = :birthday WHERE id = :id")
    suspend fun updateBirthdayInfo(id: String, birthday: String)

    @Query("UPDATE user SET heartRate = :heartRate WHERE id = :id")
    suspend fun updateHeartRate(id: String, heartRate: Int)

    @Query("UPDATE user SET languageIndex = :languageIndex WHERE id = :id")
    suspend fun updateLanguageInfo(id: String, languageIndex: Int):Int

    @Query("UPDATE user SET gender = :gender WHERE id = :id")
    suspend fun updateGenderInfo(id: String, gender: Int)

    @Query("UPDATE user SET isFirstOpen = :isFirst WHERE id = :id")
    suspend fun updateIsFirstOpen(id: String, isFirst: Boolean):Int

    @Query("SELECT isFirstOpen FROM user WHERE id = :id")
    suspend fun getIsFirstOpen(id: String): Boolean

    @Query("UPDATE user SET isFirstAuthorize = :isFirstAuthorize WHERE id = :id")
    suspend fun updateIsFirstAuthorizePermission(id: String, isFirstAuthorize: Boolean)

    @Query("SELECT isFirstAuthorize FROM user WHERE id = :id")
    suspend fun getIsFirstAuthorize(id: String): Boolean

    @Query("SELECT languageIndex FROM user WHERE id = :id")
    suspend fun getLanguagePosition(id: String): Int

    //    @Query("UPDATE user SET isFirstDetection = :isFirstDetection WHERE id = :id")
    @Query("UPDATE user SET isFirstDetection = CASE WHEN id = :id THEN :isFirstDetection ELSE isFirstDetection END WHERE id = :id")
    suspend fun updateIsFirstOpenGuide(id: String, isFirstDetection: Boolean): Int

    @Query("SELECT isFirstDetection FROM user WHERE id = :id")
    suspend fun getIsFirstOpenGuide(id: String): Boolean

    //事务，关联user表
//    @Transaction
//    @Query("SELECT * FROM user WHERE id = :userId")
//    fun getUserWithHeartRates(userId: String): List<UserWithHeartRates>
}
